<template>
    <div class="unit-init-wrap">
        <el-row type="flex" justify="space-between">
            <el-form :inline="true" :model="searchForm" class="demo-form-inline" @submit.native.prevent>
                <el-form-item>
                    <el-input v-model.trim="searchForm.name" placeholder="请输入业主单位名称" @keyup.enter.native="getTableData(1)"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select  v-model="searchForm.areaId" placeholder="请选择区域" filterable clearable @change="getTableData(1)">
                        <el-option
                            v-for="item in areaList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button class="form-search-btn" type="primary" @click="getTableData(1)">查询</el-button>
                </el-form-item>
            </el-form>
            <div class="more-btns">
                <el-button type="primary" @click="addUnit">新增</el-button>
            </div>
        </el-row>
        <el-table
            v-loading="loading"
            ref="unitTableList"
            :data="tableData"
            style="width: 100%">
            <el-table-column label="联网用户名称" prop="shortName"></el-table-column>
            <el-table-column label="排序号" prop="sort" width="80"></el-table-column>
            <el-table-column label="维保公司" prop="maintainComName"></el-table-column>
            <el-table-column label="视频截图个数" prop="picLimit" width="120"></el-table-column>
            <!-- <el-table-column label="高危防火单位" prop="fireType" width="150">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.fireType"
                        active-text="是"
                        inactive-text="否"
                        :active-value=2
                        :inactive-value=1
                        @change="changeFireType(scope.row)">
                    </el-switch>
                </template>
            </el-table-column> -->
            <el-table-column label="系统状态" prop="systemStatus" width="200">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.systemStatus"
                        active-text="正常"
                        inactive-text="暂停"
                        :active-value=1
                        :inactive-value=0
                        @change="changeSystemStatus(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column label="开启电话提醒" prop="isRemind" width="150">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.isRemind"
                        active-text="是"
                        inactive-text="否"
                        :active-value=1
                        :inactive-value=0
                        @change="changeRemind(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="200">
                <template slot-scope="scope">
                    <el-button @click="edit(scope.row)" type="text" size="small">编辑</el-button>
                    <span class="dividing-line">|</span>
                    <el-button @click="configUnit(scope.row)" type="text" size="small">配置</el-button>
                    <span class="dividing-line">|</span>
                    <el-button @click="configdelOrg(scope.row)" type="text" size="small">删除</el-button>
                    <span class="dividing-line">|</span>
                    <el-button @click="initUnit(scope.row)" type="text" size="small">初始化</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="currentPage"
            :page-sizes="[10, 50, 100]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
        <!-- 编辑/新增单位 -->
        <el-dialog
            :close-on-click-modal="false"
            :title="dialogUnitTxt"
            width="800px"
            top='10vh'
            v-if="dialogUnitShow"
            :visible.sync="dialogUnitShow"
            @close="closeUnit"
            v-dialogDrag>
            <unit-info-form :oldInfo="editInfo" :closeFun="closeUnit"></unit-info-form>
        </el-dialog>
        <!-- 配置单位 -->
        <el-dialog
            :close-on-click-modal="false"
            title="配置"
            width="600px"
            top='10vh'
            v-if="dialogConfigShow"
            :visible.sync="dialogConfigShow"
            @close="closeConfig"
            v-dialogDrag>
            <config-info-form :oldInfo="configInfo" :closeFun="closeConfig"></config-info-form>
        </el-dialog>
    </div>
</template>
<script>
import { getAreaCounty } from '@/api/area'
import { initUserOrgInfo, delOrg } from '@/api/user'
import * as Dao from '@/api/org'
import unitInfoForm from './Dialog/UnitInfoForm'
import configInfoForm from './Dialog/ConfigInfoForm'

export default {
    name: 'UnitInit',
    components: {
        unitInfoForm,
        configInfoForm
    },
    data() {
        return {
            loading: false,
            areaList: [],
            searchForm: {
                name: '',
                areaId: ''
            },
            tableData: [],
            // 分页
            currentPage: 1,
            pageSize: 10,
            total: 0,
            dialogUnitTxt: '新增业主单位',
            dialogUnitShow: false,
            editInfo: {},
            dialogConfigShow: false,
            configInfo: {},
        }
    },
    created() {
        let that = this
        // 获取区域下拉框数据
        that.getAreaOptions()
        that.getTableData()
    },
    methods: {
        // 获取区域下拉框数据
        getAreaOptions() {
            let that = this
            let parames = {
                parentId: '',
 				pageIndex: 0,
 				pageSize: 10000,
 				queryType: 0
            }
            getAreaCounty(parames).then(res => {
                that.areaList = res.data
            })
        },
        // 获取列表数据
        getTableData(pageIndex, pageSize) {
            let that = this
            that.loading = true
            if (pageIndex) {
                that.currentPage = pageIndex
            }
            if (pageSize) {
                that.pageSize = pageSize
            }
            let parames = {
                name: that.searchForm.name.trim(),
                areaId: that.searchForm.areaId || 0,
                queryType: 1,
                pageSize: that.pageSize,
                pageIndex: that.currentPage - 1
            }
            // 获取列表信息
            Dao.getListForAdminSet(parames).then(res => {
                that.tableData = res.data
                that.total = res.total
                that.loading = false
            })
        },
        handleSizeChange(pageSize) {
            this.getTableData(1, pageSize)
        },
        handleCurrentChange(currentPage) {
            this.getTableData(currentPage)
        },
        // 改变是否重点单位
        changeIsEmphasis(row) {
            let that = this
            this.$confirm('确定要修改吗？', '修改重点业主单位', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                roundButton: true
            }).then(() => {
                Dao.updateIsEmp({
                    isEmp: row.isEmphasis,
                    organizationId: row.organizationId
                }).then(res => {
                    if( res.status === 200) {
                        that.$message({
                            type: 'success',
                            message: '修改成功!'
                        })
                    } else {
                        that.$message.warning(res.info)
                    }
                })
            }).catch(() => {
                row.isEmphasis = row.isEmphasis == 0 ? 1 : 0
            })
        },
        // 改变是否高危防火单位
        changeFireType(row) {
            let that = this
            this.$confirm('确定要修改吗？', '修改高危防火单位', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                roundButton: true
            }).then(() => {
                Dao.updateUcttuFireType({
                    fireType: row.fireType,
                    organizationId: row.organizationId
                }).then(res => {
                    if( res.status === 200) {
                        that.$message({
                            type: 'success',
                            message: '修改成功!'
                        })
                    } else {
                        that.$message.warning(res.info)
                    }
                })
            }).catch(() => {
                row.fireType = row.fireType == 1 ? 2 : 1
            })
        },
        // 改变系统状态
        changeSystemStatus(row) {
            let that = this
            this.$confirm('确定要修改吗？', '修改系统状态', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                roundButton: true
            }).then(() => {
                Dao.updateStatus({
                    status: row.systemStatus,
                    organizationId: row.organizationId
                }).then(res => {
                    if( res.status === 200) {
                        that.$message({
                            type: 'success',
                            message: '修改成功!'
                        })
                    } else {
                        that.$message.warning(res.info)
                    }
                })
            }).catch(() => {
                row.systemStatus = row.systemStatus == 0 ? 1 : 0
            })
        },
        // 改变提醒
        changeRemind(row) {
            let that = this
            this.$confirm('确定要修改吗？', '修改开启提醒', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                roundButton: true
            }).then(() => {
                Dao.updateIsRemind({
                    isRemind: row.isRemind,
                    organizationId: row.organizationId
                }).then(res => {
                    if( res.status === 200) {
                        that.$message({
                            type: 'success',
                            message: '修改成功!'
                        })
                    } else {
                        that.$message.warning(res.info)
                    }
                })
            }).catch(() => {
                row.isRemind = row.isRemind == 0 ? 1 : 0
            })
        },
        // 新增业主单位
        addUnit() {
            let that = this
            that.editInfo = {}
            that.dialogUnitTxt = '新增业主单位'
            that.dialogUnitShow = true
        },
        // 配置业主单位
        configUnit(row) {
            let that = this
            that.configInfo = row
            that.dialogConfigShow = true
        },
        closeConfig() {
            let that = this
            that.dialogConfigShow = false
        },
        // 编辑
        edit(row) {
            let that = this
            that.editInfo = row
            that.dialogUnitTxt = '编辑业主单位'
            that.dialogUnitShow = true
        },
        closeUnit(flag) {
            let that = this
            that.dialogUnitShow = false
            if(typeof(flag) !== 'object' && flag) {
                that.getTableData(this.editInfo.id?'':1)
            }
        },
        // 删除 
        configdelOrg(row) {
            let that = this
            this.$confirm('确定要删除吗？', '温馨提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                roundButton: true
            }).then(() => {
                console.log('11111111', row.organizationId)
                delOrg({
                    orgId: row.organizationId
                }).then(res => {
                    if( res.status === 200) {
                        that.$message({
                            type: 'success',
                            message: '删除成功!'
                        })
                        that.getTableData(1)
                    } else {
                        that.$message.warning(res.info)
                    }
                })
            }).catch(() => {
                
            })
        },
        // 初始化用户单位
        initUnit(row) {
            let that = this
            initUserOrgInfo({
                orgId: row.organizationId
            }).then(res => {
                if( res.status === 200) {
                    that.$message({
                        type: 'success',
                        message: '初始化成功!'
                    })
                } else {
                    that.$message.warning(res.info)
                }
            })
        },
    }
}
</script>
